package com.codefish.controller;

import java.time.LocalDateTime;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.codefish.entity.OutMoney;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.service.OutMoneyService;
import com.codefish.util.Assert;
import com.codefish.util.JWTService;
import com.codefish.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 支出
 *
 * @author codefish
 * @email cool_fish@aliyun.com
 * @date 2021-07-15 15:43:57
 */
@RestController
@RequestMapping("/outMoney")
public class OutMoneyController {
    @Autowired
    private OutMoneyService outMoneyService;


    /**
     * 新增
     *
     * @param token
     * @param outMoney
     * @return
     */
    @PostMapping("/saveOne")
    public R saveOne(@RequestHeader("Authentication") String token
            , @RequestBody OutMoney outMoney) {
        Assert.isNull(outMoney.getOutDate(), "outDate不可为空");
        Assert.isNull(outMoney.getOutMoneyType(), "outMoneyType不可为空");
        Assert.isNull(outMoney.getCapitalAccountId(), "capitalAccountId不可为空");
        Assert.isNull(outMoney.getOutAmount(), "outAmount无可为空");
        Assert.isNull(outMoney.getRemark(), "remark不可为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");
        Assert.isNull(user.getFamilyId(), "当前用户家庭信息无效");

        // set createId / createTime / familyId
        outMoney.setCreateId(user.getId());
        outMoney.setCreateTime(LocalDateTime.now());
        outMoney.setFamilyId(user.getFamilyId());

        int id = outMoneyService.saveOne(outMoney);
        if (ObjectUtil.isNotEmpty(id) && id >= 1) {
            return R.ok("新增成功").put("id", id);
        }


        return R.error("新增失败");
    }

    /**
     * 修改
     *
     * @param token
     * @param outMoney
     * @return
     */
    @PostMapping("/updateOne")
    public R updateOne(@RequestHeader("Authentication") String token
            , @RequestBody OutMoney outMoney) {
        Assert.isNull(outMoney.getId(), "id不可为空");
        Assert.isNull(outMoney.getOutDate(), "outDate不可为空");
        Assert.isNull(outMoney.getOutMoneyType(), "outMoneyType不可为空");
        Assert.isNull(outMoney.getCapitalAccountId(), "capitalAccountId不可为空");
        Assert.isNull(outMoney.getOutAmount(), "outAmount无可为空");
        Assert.isNull(outMoney.getRemark(), "remark不可为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");

        //set updateId / updateTime
        outMoney.setUpdateId(user.getId());
        outMoney.setUpdateTime(LocalDateTime.now());

        boolean flag = outMoneyService.updateOne(outMoney);

        if (flag) {
            return R.ok("修改成功");
        }

        return R.error("修改失败");
    }

    /**
     * 根据id获取详情
     *
     * @param token
     * @param id
     * @return
     */
    @GetMapping("/getItemById")
    public R getItemById(@RequestHeader("Authentication") String token
            , @RequestParam("id") Integer id) {

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");

        // 确认当前用户是否可以访问此条支出记录
        boolean checkFlag = outMoneyService.checkQualification(user.getId(), id);
        if (!checkFlag) {
            return R.error("没有查询此条支出记录的资格");
        }

        // 查询
        OutMoney outMoney = outMoneyService.getItemById(id);
        if (ObjectUtils.isNotEmpty(outMoney)) {
            return R.ok().put("data", outMoney);
        }

        return R.error("查询失败");
    }

    /**
     * 删除
     *
     * @param token
     * @param id
     * @return
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestHeader("Authentication") String token
            , @RequestParam("id") Integer id) {

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");

        // 确认当前用户是否可以访问此条支出记录
        boolean checkFlag = outMoneyService.checkQualification(user.getId(), id);
        if (!checkFlag) {
            return R.error("没有删除此条支出记录的资格");
        }

        boolean flag = outMoneyService.deleteOne(id);

        if (flag) {
            return R.ok("删除成功");
        }

        return R.error("删除失败");
    }

    /**
     * 条件分页查询
     * @param token
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPageByParams")
    public R getPageByParams(@RequestHeader("Authentication") String token
            , @RequestParam("pageNo") Integer pageNo
            , @RequestParam("pageSize") Integer pageSize) {

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");
        Assert.isNull(user.getFamilyId(), "当前用户家庭信息无效");

       IPage<OutMoney> retPage =  outMoneyService.getPageByParams(user.getFamilyId(),pageNo,pageSize);

       if (ObjectUtils.isNotEmpty(retPage)){
           return R.ok().put("data",retPage);
       }

        return R.error("查询失败");
    }
}
